<template>
	<view>
		
		<uni-swipe-action ref="swipeAction" v-if="listData.length>0">
			<uni-swipe-action-item v-for="(item,index) in listData" :key="item.id" :right-options="swipeitem.options"
				@change="swipeChange($event, index)" @click="swipeClick($event, index)">
				<view
					@click="tokefumini(item.latestMessage.content.extra?item.latestMessage.content.extra.name:'',item.latestMessage.content.extra?item.latestMessage.content.extra.icon:'',item.targetId?item.targetId:'')">
					<view class="uni-list-chat__container">
						<view class="uni-list-chat__header ">
							<image class="uni-list-chat__header-image"
								:src="item.latestMessage.content.extra.fromId==memberId?item.latestMessage.content.extra.icon:item.latestMessage.content.extra.avatar"
								mode="aspectFill"></image>
						</view>
						<view  v-if=" item.unreadMessageCount>0" class="uni-list-chat__badge uni-list-chat__badge-pos">
							<text
								class="uni-list-chat__badge-text">{{ item.unreadMessageCount>0?item.unreadMessageCount:'' }}</text>
						</view>
						<view class="uni-list-chat__content">
							<view class="uni-list-chat__content-main">
								<text
									class="uni-list-chat__content-title uni-ellipsis">{{ item.latestMessage.content.extra?item.latestMessage.content.extra.name:'' }}</text>
								<text
									class="uni-list-chat__content-note uni-ellipsis">{{ item.latestMessage.content.content }}</text>
							</view>
							<view>
									<text class="uni-list-chat__content-extra">{{ toTime(item.latestMessage.sentTime) }}</text>
							</view>
						</view>
					</view>
				</view>
			</uni-swipe-action-item>
		</uni-swipe-action>
		<ljshop-empty v-else :image="'https://image.hzwltb69.com/app/images/nonews.png'" tipText="暂无消息~">
		</ljshop-empty>
	</view>
</template>

<script>
	import {
		mapMutations,
		mapActions,
		mapState,
		mapGetters
	} from 'vuex';
	//ryGlobalData是全局，去main.js
	export default {
		data() {
			return {
				userinfo: uni.getStorageSync("userinfo"),
				UID: uni.getStorageSync("UId"),

				inputHeight: '0rpx',
				listData: [],
				memberId:"",
				swipeitem: {
					id: 2,
					options: [
						// {
						// 	text: '置顶'
						// },
						// {
						// 	text: '标记为已读',
						// 	style: {
						// 		backgroundColor: 'rgb(254,156,1)'
						// 	}
						// },
						{
							text: '删除',
							style: {
								backgroundColor: 'rgb(255,58,49)'
							}
						}
					],
					content: 'item3'
				}

			}
		},
		onShow(e) {
			this.memberId = "member" + uni.getStorageSync("UId")
			this.connectRy();
		},
		methods: {

			connectRy() {
				console.info()
				//判断是否连接，连接了直接获取聊天列表
				if (this.ryGlobalData.ryStatus.status !== 99) {
					var im = this.ryGlobalData.ryStatus.RongIMLib
					let _t = this
				}

				let that = this;
				let token = ""
				//uni.showLoading({})
				that.$http(
					'common.rongyun', {
						memberId: uni.getStorageSync("UId"),
						refresh: false
					},
				).then(res => {

					if (res.code == 200) {
						token = res.token;
						this.ryConnet(token)
					}
				});
				//this.ryConnet()		 
			},
			ryConnet(token) { // 连接
				var im = this.ryGlobalData.ryStatus.RongIMLib
				console.log(im);
				let _t = this
				//token，去找后台要，不给就锤他
				let ry_token = token
				//let ry_token="hwqrppVQNicng1peuiTz9JGeaCBDOoa2MKx0svgqBPA=@dxbv.cn.rongnav.com;dxbv.cn.rongcfg.com"
				im.connect(token).then((user) => {
					console.info(JSON.stringify(user))
					_t.getMsgList()
					_t.getMsgCount()
				}).catch(function(error) {

				});
			},
			getMsgList() { //获取历史消息列表
				let _t = this
				const RongIMLib = this.ryGlobalData.ryStatus.RongIMLib

				const startTime = 0;
				const count = 100;
				const order = 0;

				RongIMLib.getConversationList({
					count: count,
					startTime: startTime,
					order: order
				}).then(res => {
					if (res.code === 0) {
						//  console.log(res.code, res.data)
						this.listData = res.data
						console.log( JSON.stringify(res.data))
					} else {
						console.log(res.code, res.msg)
					}
				})

			},
			tokefumini(name, logo, bussid) {

				let that = this;
				let Id = ""
				let path = ""
				console.info(bussid)
				if (bussid.indexOf("business") != -1) {
					path = "/pages/kefu/minikefu"
					Id = bussid.slice(8)
					console.info(Id)
				}
				if (bussid.indexOf("admin") != -1) {
					path = "/pages/kefu/minipingtaikefu"
					Id = bussid.slice(5)
					console.info(Id)
				}
				this.$Router.push({
					path: path,
					//path: '/pages/kefu/index',
					query: {
						id: Id,
						logo: logo ? logo : '',
						name: name ? name : '',
					}
				})
			},


			toTime(time) {
				var date = new Date(time);
				var Y = date.getFullYear() + "-";
				var M =
					(date.getMonth() + 1 < 10 ?
						"0" + (date.getMonth() + 1) :
						date.getMonth() + 1) + "-";
				var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
				var h = date.getHours() + ":";
				var m = date.getMinutes() + ":";
				var s = date.getSeconds();
				return Y + M + D + h + m + s;
			},
			swipeClick(e, index) {
				var im = this.ryGlobalData.ryStatus.RongIMLib
				
                const conversationType = im.ConversationType.PRIVATE;
				
				let item= this.listData[index]
				
				const targetId = item.targetId;
				console.info(targetId)
				console.info(conversationType)
			 
				let {
					content
				} = e;
				if (content.text === '置顶') {
					
					const isTop = true
					im.setConversationToTop({
					    conversationType,
					    targetId,
					}, isTop).then(({code}) => {
					    console.info(code)
					    
					})
				} 
				if (content.text === '标记为已读') {
				 
				} 
				if (content.text === '删除') {
				 im.removeConversation({
				     conversationType,
				     targetId: targetId,
				 }).then(res => {
				     // 删除指定会话成功
				     if(res.code === 0){
				        // console.log(res.code)
						this.getMsgList();
				     } else {
				        // console.log(res.code, res.msg)
				     }
				 })
				} 
				 console.info(content.text)
				
			},
			swipeChange(e, index) {
				
			},
			getMsgCount(){
				let _t = this
				const RongIMLib = this.ryGlobalData.ryStatus.RongIMLib
				
				const startTime = 0;
				const count = 20;
				const order = 0;
				
				const includeMuted = false
				const conversationTypes = [RongIMLib.ConversationType.PRIVATE, RongIMLib.ConversationType.GROUP]
				RongIMLib.getTotalUnreadCount(includeMuted, conversationTypes).then(res => {
				  if (res.code === 0) {
				    console.log(res.data)
					if (res.data>0) {
						if(res.data<100){
							uni.setStorageSync("UnreadCount", res.data)
						}
						else{
							uni.setStorageSync("UnreadCount", "99")
						}
						uni.setTabBarBadge({
							index: 2,
							text:res.data<100?String(res.data):"99"
						})
					} else {
						uni.removeTabBarBadge({
							index: 2
						})
					}
					
				  } else {
				   
				  }
				}).catch(error => {
				  console.log(error)
				})
			}
			
		}
	}
</script>

<style>
	page {
		background-color: f1f1f1;
	}

	.chat-custom-right {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: space-between;
		align-items: flex-end;
	}

	.chat-custom-text {
		font-size: 12px;
		color: #999;
	}

	.content-box {
		flex: 1;
		/* #ifdef APP-NVUE */
		justify-content: center;
		/* #endif */
		height: 44px;
		line-height: 44px;
		padding: 0 15px;
		position: relative;
		background-color: #fff;
		border-bottom-color: #f5f5f5;
		border-bottom-width: 1px;
		border-bottom-style: solid;
	}

	.content-text {
		font-size: 15px;
	}

	.example-body {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
		padding: 10px 0;
		background-color: #fff;
	}

	.button {
		border-color: #e5e5e5;
		border-style: solid;
		border-width: 1px;
		padding: 4px 8px;
		border-radius: 4px;
	}

	.button-text {
		font-size: 15px;
	}

	.slot-button {
		/* #ifndef APP-NVUE */
		display: flex;
		height: 100%;
		/* #endif */
		flex: 1;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 0 20px;
		background-color: #ff5a5f;
	}

	.slot-button-text {
		color: #ffffff;
		font-size: 14px;
	}
	.uni-list-chat__container {
		position: relative;
		background: #fff;
		display: flex;
		flex-direction: row;
		flex: 1;
		margin-top: 2rpx;
		padding:20rpx 30rpx;
		overflow: hidden;
	}
	
	.uni-list-chat__header {
		
		display: flex;
		align-content: center;
		
		flex-direction: row;
		justify-content: center;
		align-items: center;
		flex-wrap: wrap-reverse;
	
		width: 50px;
		height: 50px;
		border-radius: 5px;
		border-color: 5px;
		border-width: 1px;
		border-style: solid;
		overflow: hidden;
	}
	.uni-list-chat__header-image {
		width: 50px;
		height: 50px;
	}
	.uni-list-chat__badge-pos {
		position: absolute;
		top: 6rpx;
		left: 100rpx;
		width: auto;
		height: 36rpx;
		padding: 0 12rpx;
	}
	.uni-list-chat__badge {
	
		display: flex;
		
		justify-content: center;
		align-items: center;
		border-radius: 100px;
		background-color: #ff5a5f;
	}
	.uni-list-chat__badge-text {
		color: #fff;
		font-size: 24rpx;
	 }
	.uni-list-chat__content {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		flex: 1;
		overflow: hidden;
		padding: 2px 0;
	}
	
	.uni-list-chat__content-main {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: space-between;
		padding-left: 10px;
		flex: 1;
		overflow: hidden;
	}
	
	.uni-list-chat__content-title {
		font-size: 30rpx;
		color: #3b4144;
		font-weight: bold;
		overflow: hidden;
	}
	
	.uni-list-chat__content-note {
		margin-top: 6rpx;
		color: #999;
		font-size: 24rpx;
		font-weight: normal;
		overflow: hidden;
	}
 
	.uni-list-chat__content-extra {
		color: #999;
		font-size: 22rpx;
		font-weight: normal;
		overflow: hidden;
		flex-direction: column;
		justify-content: space-between;
		align-items: flex-end;
		margin-left: 10rpx;
		
	}
	.uni-ellipsis {
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		lines: 1;
	}
	 
</style>